const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

function formatTimeTwo(number, format) {

  var formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
  var returnArr = [];

  var date = new Date(number * 1000);
  returnArr.push(date.getFullYear());
  returnArr.push(formatNumber(date.getMonth() + 1));
  returnArr.push(formatNumber(date.getDate()));

  returnArr.push(formatNumber(date.getHours()));
  returnArr.push(formatNumber(date.getMinutes()));
  returnArr.push(formatNumber(date.getSeconds()));

  for (var i in returnArr) {
    format = format.replace(formateArr[i], returnArr[i]);
  }
  return format;
}

const formatDate = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()

  return [year, month, day].map(formatNumber).join('/')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

const getRandomColor = () => {
  let rgb = []
  for (let i = 0; i < 3; ++i) {
    let color = Math.floor(Math.random() * 256).toString(16)
    color = color.length == 1 ? '0' + color : color
    rgb.push(color)
  }
  return '#' + rgb.join('')
}

const getTouchData = (endX, endY, startX, startY) => {
  let turn = "";
  if (endX - startX > 20 && Math.abs(endY - startY) < 70) {      //右滑
    turn = "right";
  } else if (endX - startX < -20 && Math.abs(endY - startY) < 70) {   //左滑
    turn = "left";
  }
  return turn;
}

const getWrapName = (val) =>{
  let name = ''
  switch (val) {
    case "1":
      name = '散酒'
      break
    case "2":
      name = '裸瓶'
      break
    case "3":
      name = '纸盒'
      break
    case "4":
      name = '木盒'
      break
    default:
      name = val
      break
  }
  return name
}

module.exports = {
  formatTime: formatTime,
  formatDate: formatDate,
  getRandomColor: getRandomColor,
  formatTimeTwo,
  getTouchData,
  getWrapName
}
